package com.miyake.loveviewweb.client.chart;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.miyake.loveviewweb.client.DataServiceWrapper;
import com.miyake.loveviewweb.shared.model.ChartModel;

public class StaticChart extends Composite implements IChart {
    public interface StaticChartHandler {
        void onSelect(StaticChart chart);
    };
    
    private Image image;
    private ChartModel chartModel;

    public StaticChart(final StaticChartHandler handler) {
        VerticalPanel panel = new VerticalPanel();
        initWidget(panel);
        image = new Image();
        panel.add(image);   
        if (handler != null) {
            image.addClickHandler(new ClickHandler() {

                public void onClick(ClickEvent event) {
                    handler.onSelect(StaticChart.this);
                }
                
            });
        }
    }
    public void setData(ChartModel model) {
        this.chartModel = model;
        DataServiceWrapper.getInstance().getService().getChart(model.getKey(), true, new AsyncCallback<ChartModel>() {

            public void onFailure(Throwable caught) {
                // TODO Auto-generated method stub
                
            }

            public void onSuccess(ChartModel result) {
                image.setUrl(result.getChartURL());
            }
            
        });
    }

    public void setChartSize(Integer width, Integer height) {
        image.setSize(width.toString()+"px", height+"px");
        new ChartSizeUpdater(this.chartModel.getKey(), width, height) {

            @Override
            void onComplete(ChartModel chartModel) {
                image.setUrl(chartModel.getChartURL());
            }

            
        };
    }

    public void update() {
        // TODO Auto-generated method stub
        
    }

    public void clearPlot(int i) {
        // TODO Auto-generated method stub
        
    }

    public Widget getUiWidget() {
        return this;
    }
    


}
